Modification start date
[BattleCats.git] / Assets / Scripts / Imported / Tilemap / Brushes / Tint Brush Smooth / Scripts / TintTextureGenerator.cs
blobc2791283996d6a312619bce9f98cecdf12fb3735
1 using System.ComponentModel;
2 using UnityEngine;
3 using UnityEngine.Tilemaps;
4 [ExecuteInEditMode]
5 public class TintTextureGenerator : MonoBehaviour
7 public int k_TintMapSize = 256;
9 public void Start()
11 Refresh(GetComponent<Grid>());
14 private Texture2D m_TintTexture;
15 private Texture2D tintTexture
17 get
19 if (m_TintTexture == null)
21 m_TintTexture = new Texture2D(k_TintMapSize, k_TintMapSize, TextureFormat.ARGB32, false);
22 m_TintTexture.hideFlags = HideFlags.HideAndDontSave;
23 m_TintTexture.wrapMode = TextureWrapMode.Clamp;
24 m_TintTexture.filterMode = FilterMode.Bilinear;
25 RefreshGlobalShaderValues();
27 return m_TintTexture;
31 public void Refresh(Grid grid)
33 if (grid == null)
34 return;
36 int w = tintTexture.width;
37 int h = tintTexture.height;
38 for (int y = 0; y < h; y++)
40 for (int x = 0; x < w; x++)
42 Vector3Int world = TextureToWorld(new Vector3Int(x, y, 0));
43 tintTexture.SetPixel(x, y, GetGridInformation(grid).GetPositionProperty(world, "Tint", Color.white));
46 tintTexture.Apply();
49 public void Refresh(Grid grid, Vector3Int position)
51 if (grid == null)
52 return;
54 RefreshGlobalShaderValues();
55 Vector3Int texPosition = WorldToTexture(position);
56 tintTexture.SetPixel(texPosition.x, texPosition.y, GetGridInformation(grid).GetPositionProperty(position, "Tint", Color.white));
57 tintTexture.Apply();
60 public Color GetColor(Grid grid, Vector3Int position)
62 if (grid == null)
63 return Color.white;
65 return GetGridInformation(grid).GetPositionProperty(position, "Tint", Color.white);
68 public void SetColor(Grid grid, Vector3Int position, Color color)
70 if (grid == null)
71 return;
73 GetGridInformation(grid).SetPositionProperty(position, "Tint", color);
74 Refresh(grid, position);
77 Vector3Int WorldToTexture(Vector3Int world)
79 return new Vector3Int(world.x + tintTexture.width / 2, world.y + tintTexture.height / 2, 0);
82 Vector3Int TextureToWorld(Vector3Int texpos)
84 return new Vector3Int(texpos.x - tintTexture.width / 2, texpos.y - tintTexture.height / 2, 0);
87 GridInformation GetGridInformation(Grid grid)
89 GridInformation gridInformation = grid.GetComponent<GridInformation>();
91 if (gridInformation == null)
92 gridInformation = grid.gameObject.AddComponent<GridInformation>();
94 return gridInformation;
97 void RefreshGlobalShaderValues()
99 Shader.SetGlobalTexture("_TintMap", m_TintTexture);
100 Shader.SetGlobalFloat("_TintMapSize", k_TintMapSize);